Imports System.ComponentModel
Imports ComprUtils

Public Class Form1
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  Friend WithEvents btn1 As System.Windows.Forms.Button
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents btn2 As System.Windows.Forms.Button
  Friend WithEvents btnUnzip As System.Windows.Forms.Button
  Friend WithEvents StatusStrip1 As System.Windows.Forms.StatusStrip
  Friend WithEvents tsPB1 As System.Windows.Forms.ToolStripProgressBar
  Friend WithEvents tsSB1 As System.Windows.Forms.ToolStripStatusLabel
  Friend WithEvents tsPB2 As System.Windows.Forms.ToolStripProgressBar
  Friend WithEvents bgUnpackMassive As MassiveUncompress
  Friend WithEvents bgUnzipFile As UnZip
  Friend WithEvents bgUnrarFile As UnRar
  Friend WithEvents fbd1 As System.Windows.Forms.FolderBrowserDialog
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
    Me.TextBox1 = New System.Windows.Forms.TextBox
    Me.btn1 = New System.Windows.Forms.Button
    Me.Label1 = New System.Windows.Forms.Label
    Me.TextBox2 = New System.Windows.Forms.TextBox
    Me.Label2 = New System.Windows.Forms.Label
    Me.btn2 = New System.Windows.Forms.Button
    Me.btnUnzip = New System.Windows.Forms.Button
    Me.fbd1 = New System.Windows.Forms.FolderBrowserDialog
    Me.StatusStrip1 = New System.Windows.Forms.StatusStrip
    Me.tsSB1 = New System.Windows.Forms.ToolStripStatusLabel
    Me.tsPB1 = New System.Windows.Forms.ToolStripProgressBar
    Me.tsPB2 = New System.Windows.Forms.ToolStripProgressBar
    Me.bgUnpackMassive = New MassiveUnzip.MassiveUncompress
    Me.bgUnzipFile = New ComprUtils.UnZip
    Me.bgUnrarFile = New ComprUtils.UnRar
    Me.StatusStrip1.SuspendLayout()
    Me.SuspendLayout()
    '
    'TextBox1
    '
    Me.TextBox1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.TextBox1.Location = New System.Drawing.Point(8, 40)
    Me.TextBox1.Name = "TextBox1"
    Me.TextBox1.Size = New System.Drawing.Size(490, 20)
    Me.TextBox1.TabIndex = 0
    '
    'btn1
    '
    Me.btn1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.btn1.Location = New System.Drawing.Point(504, 40)
    Me.btn1.Name = "btn1"
    Me.btn1.Size = New System.Drawing.Size(24, 23)
    Me.btn1.TabIndex = 2
    Me.btn1.Text = "..."
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(8, 21)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(200, 16)
    Me.Label1.TabIndex = 3
    Me.Label1.Text = "Source folder"
    '
    'TextBox2
    '
    Me.TextBox2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.TextBox2.Location = New System.Drawing.Point(8, 104)
    Me.TextBox2.Name = "TextBox2"
    Me.TextBox2.Size = New System.Drawing.Size(490, 20)
    Me.TextBox2.TabIndex = 0
    '
    'Label2
    '
    Me.Label2.Location = New System.Drawing.Point(8, 85)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(200, 16)
    Me.Label2.TabIndex = 3
    Me.Label2.Text = "Destination folder"
    '
    'btn2
    '
    Me.btn2.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.btn2.Location = New System.Drawing.Point(504, 104)
    Me.btn2.Name = "btn2"
    Me.btn2.Size = New System.Drawing.Size(24, 23)
    Me.btn2.TabIndex = 2
    Me.btn2.Text = "..."
    '
    'btnUnzip
    '
    Me.btnUnzip.Location = New System.Drawing.Point(195, 145)
    Me.btnUnzip.Name = "btnUnzip"
    Me.btnUnzip.Size = New System.Drawing.Size(149, 23)
    Me.btnUnzip.TabIndex = 4
    Me.btnUnzip.Text = "Unpack files"
    '
    'StatusStrip1
    '
    Me.StatusStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.tsSB1, Me.tsPB1, Me.tsPB2})
    Me.StatusStrip1.Location = New System.Drawing.Point(0, 192)
    Me.StatusStrip1.Name = "StatusStrip1"
    Me.StatusStrip1.Size = New System.Drawing.Size(540, 22)
    Me.StatusStrip1.TabIndex = 5
    Me.StatusStrip1.Text = "StatusStrip1"
    '
    'tsSB1
    '
    Me.tsSB1.Name = "tsSB1"
    Me.tsSB1.Size = New System.Drawing.Size(525, 17)
    Me.tsSB1.Spring = True
    '
    'tsPB1
    '
    Me.tsPB1.Name = "tsPB1"
    Me.tsPB1.Size = New System.Drawing.Size(100, 16)
    Me.tsPB1.Step = 1
    Me.tsPB1.Visible = False
    '
    'tsPB2
    '
    Me.tsPB2.Name = "tsPB2"
    Me.tsPB2.Size = New System.Drawing.Size(100, 16)
    Me.tsPB2.Visible = False
    '
    'bgUnpackMassive
    '
    Me.bgUnpackMassive.SourceDir = Nothing
    Me.bgUnpackMassive.TargetDir = Nothing
    Me.bgUnpackMassive.TestMode = False
    Me.bgUnpackMassive.UnRar = Nothing
    Me.bgUnpackMassive.Unzip = Nothing
    Me.bgUnpackMassive.WorkerReportsProgress = True
    Me.bgUnpackMassive.WorkerSupportsCancellation = True
    '
    'bgUnzipFile
    '
    Me.bgUnzipFile.PackFilename = Nothing
    Me.bgUnzipFile.TargetDir = Nothing
    Me.bgUnzipFile.WorkerReportsProgress = True
    '
    'bgUnrarFile
    '
    Me.bgUnrarFile.PackFilename = Nothing
    Me.bgUnrarFile.TargetDir = Nothing
    Me.bgUnrarFile.WorkerReportsProgress = True
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(540, 214)
    Me.Controls.Add(Me.StatusStrip1)
    Me.Controls.Add(Me.btnUnzip)
    Me.Controls.Add(Me.Label1)
    Me.Controls.Add(Me.btn1)
    Me.Controls.Add(Me.TextBox1)
    Me.Controls.Add(Me.TextBox2)
    Me.Controls.Add(Me.Label2)
    Me.Controls.Add(Me.btn2)
    Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
    Me.Name = "Form1"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Philip Software - Recursively extract zip and rar files"
    Me.StatusStrip1.ResumeLayout(False)
    Me.StatusStrip1.PerformLayout()
    Me.ResumeLayout(False)
    Me.PerformLayout()

  End Sub

#End Region

  Dim _wc As WaitCursor

  Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    My.Settings.DirSource = TextBox1.Text
    My.Settings.DirTarget = TextBox2.Text
    My.Settings.Save()
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = My.Settings.DirSource
    TextBox2.Text = My.Settings.DirTarget
    bgUnpackMassive.TestMode = False
    bgUnpackMassive.Unzip = Me.bgUnzipFile
    bgUnpackMassive.UnRar = Me.bgUnrarFile
    tsPB1.Maximum = 100 : tsPB2.Maximum = 100
    adjustInterface()
  End Sub

  Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    If fbd1.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
    TextBox1.Text = fbd1.SelectedPath
  End Sub

  Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
    If fbd1.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
    TextBox2.Text = fbd1.SelectedPath
  End Sub

  ' unzip button, asynchronously
  Private Sub btnUnzip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnzip.Click
    bgUnpackMassive.SourceDir = TextBox1.Text
    bgUnpackMassive.TargetDir = TextBox2.Text
    ' prepare interface
    If bgUnpackMassive.IsBusy Then
      Me.bgUnpackMassive.CancelAsync()
    Else
      Me.bgUnpackMassive.RunWorkerAsync()
    End If
    adjustInterface()
  End Sub
  Private Sub adjustInterface()
    Dim bWorking As Boolean = bgUnpackMassive.IsBusy
    If bWorking Then
      _wc = New WaitCursor(Me)
    Else
      If _wc IsNot Nothing Then _wc.Dispose() 'set default cursor
    End If
    tsPB1.Visible = bWorking
    tsPB2.Visible = bWorking
    If Not bWorking Then Me.tsSB1.Text = ""
    Me.btnUnzip.Text = IIf(Not bWorking, My.Resources.UnzipButton1_1, My.Resources.UnzipButton1_2)
  End Sub

#Region " Old way of do work"
  Private Sub bg_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
    ' Get the BackgroundWorker object that raised this event.
    Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
    ' get the zip utils that will do the work
    Dim zu As MassiveUncompress = CType(e.Argument, MassiveUncompress)
    zu.FindAllInDir()
    zu.ExtractAll(e)
  End Sub
#End Region

  Private Sub bgUnpackMassive_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
   Handles bgUnpackMassive.RunWorkerCompleted
    If e.Cancelled Then
      MessageBox.Show("Work was cancelled by user", "Massive Unzip", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End If
    ' MessageBox.Show("Delete zips", "Delete zips", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
    Dim frm As New DialogDeleteItems()
    frm.ListBox1.DataSource = Me.bgUnpackMassive.PackList
    If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
      If frm.CheckBox1.Checked Then bgUnpackMassive.DeleteAllInDir()
    End If
    adjustInterface()
  End Sub

  Private Sub bgUnpackMassive_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgUnpackMassive.ProgressChanged
    tsSB1.Text = CStr(e.UserState)
    tsPB1.Value = e.ProgressPercentage
  End Sub

  Dim delProgCh As ProgressChangedEventHandler = AddressOf bgUnzipFile_ProgressChanged
  Private Sub bgUnzipFile_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
    Handles bgUnzipFile.ProgressChanged, bgUnrarFile.ProgressChanged
    If Me.InvokeRequired Then
      Me.Invoke(delProgCh, sender, e)
    Else
      tsSB1.Text = CStr(e.UserState)
      tsPB2.Value = e.ProgressPercentage
    End If
  End Sub

End Class
